﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Commonality.Model;
using Commonality.DAL.MySQL;

namespace Separate.BLL
{
    public partial class InOutputForm : Form
    {
        public static Form inOutputForm;
        public InOutputForm()
        {
            InitializeComponent();
            inOutputForm = this;

        }

        private void InOutputForm_Load(object sender, EventArgs e)
        {
            Thread.Sleep(10);
            InputChartInit();
            ChartDisply();
            InputStatistics.InputStatisticsInit();
        }

        void InputChartInit()
        {
            // 设置样式
            Series Input = InputChart.Series[0];
            InputChart.Series[0].IsValueShownAsLabel = true;
            Series Abn = InputChart.Series[1];
            InputChart.Series[1].IsValueShownAsLabel = true;
            Series Mix = InputChart.Series[2];
            InputChart.Series[2].IsValueShownAsLabel = true;
            Series Block = InputChart.Series[3];
            InputChart.Series[3].IsValueShownAsLabel = true;
            Series FM = InputChart.Series[4];
            InputChart.Series[4].IsValueShownAsLabel = true;
            Series Foam = InputChart.Series[5];
            InputChart.Series[5].IsValueShownAsLabel = true;
            // 画样条
            Input.ChartType = SeriesChartType.Column;
            Abn.ChartType = SeriesChartType.Column;
            Mix.ChartType = SeriesChartType.Column;
            Block.ChartType = SeriesChartType.Column;
            FM.ChartType = SeriesChartType.Column;
            Foam.ChartType = SeriesChartType.Column;
            // 线宽2个像素
            Input.BorderWidth = 20;
            Abn.BorderWidth = 20;
            Mix.BorderWidth = 20;
            Block.BorderWidth = 20;
            FM.BorderWidth = 20;
            Foam.BorderWidth = 20;
            // 颜色
            Input.Color = Color.Blue;
            Abn.Color = Color.Red;
            Mix.Color = Color.Yellow;
            Block.Color = Color.Black;
            FM.Color = Color.Yellow;
            Foam.Color = Color.Black;
            // 图示上的文字
            Input.LegendText = "投入";
            Abn.LegendText = "异常";
            Mix.LegendText = "混料";
            Block.LegendText = "阻塞";
            FM.LegendText = "异物";
            Foam.LegendText = "泡棉异常";
            //名称
            Title InputTitle = InputChart.Titles[0];
            InputTitle.Text = "统计信息";
            // 设置显示范围
            ChartArea InputArea = InputChart.ChartAreas[0];
            //InputArea.AxisX.Minimum = 0;
            //InputArea.AxisX.Maximum = 9;               //横轴显示天数-10天
            //InputArea.AxisY.Minimum = 0;
            //InputArea.AxisY.Maximum = 20000;            //产量目标2W

            ChartArea FailInputArea = InputChart.ChartAreas[0];
            //FailInputArea.AxisX.Minimum = 0;
            //FailInputArea.AxisX.Maximum = 9;           //横轴显示天数-10天
            //FailInputArea.AxisY.Minimum = 0;
            //FailInputArea.AxisY.Maximum = 20000;
            
        }

        public void ChartDisply()
        {
            try
            {
                InputStatistics.ChartDisply(ref InputChart);
            }
            catch 
            {
            }

        }
    }

    public static class InputStatistics
    {
        public static bool ClearOperationFlag = false;
        public static int InputNumber;
        public static int AbnInputNumber;
        public static int MixInputNumber;
        public static int BlockOutNumber;
        public static int FMNumber;
        public static int FoamNumber;
        public static void InputStatisticsInit()
        {
            MySqlFunction.InputStatistics.InputStatistics_SELECT(ref InputNumber, ref AbnInputNumber,ref MixInputNumber,ref BlockOutNumber);
            MySqlFunction.InputStatistics.InputStatistics_SELECT(ref FMNumber, ref FoamNumber);
        }
        /// <summary>
        /// 产量凌晨清零
        /// </summary>
        public static void ClearOperation()
        {
            if (DateTime.Now.ToString("HH") == "00" &&( DateTime.Now.Minute >= 0|| DateTime.Now.Minute <= 10))
            {
                if (!ClearOperationFlag)
                {
                    InputNumber = 0;
                    AbnInputNumber = 0;
                    MixInputNumber = 0;
                    BlockOutNumber = 0;
                    FMNumber = 0;
                    FoamNumber = 0;
                    ClearOperationFlag = true;                    
                }

            }
            else
            {
                ClearOperationFlag = false;
            }
        }
        public static void InputAdd()
        {            
            InputNumber++;
            MySqlFunction.InputStatistics.InputStatistics_UPDATE(InputNumber, AbnInputNumber, MixInputNumber);
        }
        public static void AbnInputAdd()
        {
            AbnInputNumber++;
            MySqlFunction.InputStatistics.InputStatistics_UPDATE(InputNumber, AbnInputNumber, MixInputNumber);
        }
        public static void MixInputAdd()
        {
            MixInputNumber++;
            MySqlFunction.InputStatistics.InputStatistics_UPDATE(InputNumber, AbnInputNumber, MixInputNumber);
        }
        public static void BlockInputAdd()
        {
            BlockOutNumber++;
            MySqlFunction.InputStatistics.InputStatistics_Block_UPDATE(BlockOutNumber);
        }
        public static void FMInputAdd()
        {
            FMNumber++;
            MySqlFunction.InputStatistics.InputStatistics_FM_UPDATE(FMNumber);
        }
        public static void FoamInputAdd()
        {
            FoamNumber++;
            MySqlFunction.InputStatistics.InputStatistics_Foam_UPDATE(FoamNumber);
        }
        public static void ChartDisply( ref Chart chart)
        {
            List<string> Time = new List<string>();
            List<int> Input = new List<int>();
            List<int> AbnInput = new List<int>();
            List<int> MixInput = new List<int>();
            List<int> BlockInput = new List<int>();
            List<int> FMInput = new List<int>();
            List<int> FoamInput = new List<int>();
            //显示包含此天前10天
            for (int i = 7; i >=0; i--)
            {
                Time.Add(DateTime.Now.AddDays(-i).ToString("yyyy:MM:dd"));

                int input = 0;
                int Abn = 0;
                int Mix = 0;
                int Block = 0;
                int FM = 0;
                int Foam = 0;
                string ret1 = MySqlFunction.InputStatistics.InputStatistics_SELECT(Time[7 - i], ref input, ref Abn,ref Mix,ref Block);
                string ret2 = MySqlFunction.InputStatistics.InputStatistics_SELECT(Time[7 - i], ref FM, ref Foam);
                //Log4Net.MsgTcpInfo("-------------------------------------1" + Time[7 - i].ToString());
                //Log4Net.MsgTcpInfo(ret1);
                //Log4Net.MsgTcpInfo("-------------------------------------2");
                //Log4Net.MsgTcpInfo(ret2);
                Input.Add(2 * input);
                AbnInput.Add(2 * Abn);
                MixInput.Add(2 * Mix);
                BlockInput.Add(2 * Block);
                FMInput.Add(2 * FM);
                FoamInput.Add(2 * Foam);
            }

            chart.Series["投入"].Points.DataBindXY(Time, Input);
            chart.Series["异常"].Points.DataBindXY(Time, AbnInput);
            chart.Series["混料"].Points.DataBindXY(Time, MixInput);
            chart.Series["阻塞"].Points.DataBindXY(Time, BlockInput);
            chart.Series["异物"].Points.DataBindXY(Time, FMInput);
            chart.Series["泡棉异常"].Points.DataBindXY(Time, FoamInput);
        }
    }

}
